package com.simon.train.business.controller.admin;


import com.simon.train.business.dto.DailyTrainQueryDto;
import com.simon.train.business.dto.DailyTrainSaveDto;
import com.simon.train.business.service.DailyTrainService;
import com.simon.train.business.vo.DailyTrainQueryVO;
import com.simon.train.common.Result.Result;
import com.simon.train.common.vo.PageVO;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/admin/dailyTrain")
public class DailyTrainAdminController {

    @Resource
    private DailyTrainService dailyTrainService;

    @PostMapping("/save")
    public Result<Object> SaveDailyTrain(@Valid @RequestBody DailyTrainSaveDto dailyTrainSaveDto){
        dailyTrainService.SaveDailyTrain(dailyTrainSaveDto);
        return new Result<>();
    }

    @GetMapping("/queryList")
    public Result<PageVO<DailyTrainQueryVO>> DailyTrainQueryList(@Valid DailyTrainQueryDto dailyTrainQueryDto){
         /**
         * businessId通过线程变量获取 从而不需要由前端传入 保证了效率和安全性
         */
        PageVO<DailyTrainQueryVO> lists = dailyTrainService.QueryDailyTrainList(dailyTrainQueryDto);
        return new Result<>(lists);
    }

    @DeleteMapping("/delete/{id}")
    public Result<Object> DeleteDailyTrain(@PathVariable("id") Long id){
        dailyTrainService.deleteDailyTrain(id);
        return new Result<>();
    }

    /**
     * 通过传入日期 生成每日的车次数据
     * @param date
     * @NotBlank注解适用于string类型 不适合date类型
     * @NotNull 适用于任何类型，确保 date 不为 null
     * 经过分析 前端如果为空 传入的参数为String类型的"Invalid Date" 这个本身会报错
     * 要么前端处理一下 要么后端通过拦截器处理一下
     * @return
     */
    @GetMapping("/GenerateDaily/{date}")
    public Result<Object> GenerateDaily(
            @PathVariable
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
        dailyTrainService.GenerateDaily(date);
        return new Result<>();
    }

}
